<?php
// +----------------------------------------------------------------------
// | NewThink [ Think More,Think Better! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +----------------------------------------------------------------------
// | 版权所有：山西创泰网络科技有限公司
// +----------------------------------------------------------------------
// | Author: JiYun Date:2019-05-09 Time:9:28
// +----------------------------------------------------------------------

namespace app\common\model\system;

use app\common\model\BaseModel;
use Think\Db;

/**
 * Class SystemUser
 * @author JiYun On 2019/05/09
 * @package app\common\model\system
 */
final class SystemUser extends BaseModel
{
    protected $table = 'system_user';
    
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;
    
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 验证用户名和密码
     * @author JiYun On 2019/05/09
     * @param string $username
     * @param string $password
     * @return mixed
     */
    public function checkUser($username = '', $password = '')
    {
        $fields = 'id, username, auth';
        $fmt    = 'select ' . $fields . ' from ' . $this->table;
        $fmt    = $fmt . " where username = '%s' and password = '%s' and status = %s and delete_time = 0";
        
        $sql    = sprintf($fmt, $username, md5($password), self::STATUS_ENABLE);
        $result = Db::query($sql);
        
        return isset($result[0]) ? $result[0] : null;
    }
    
    /**
     * 更新最后登录时间和IP
     * @author JiYun On 2019/05/09
     * @param array $where
     * @param array $data
     * @return bool
     */
    public function updateLogin($where = [], $data = [])
    {
        $result = true;
        
        try {
            Db::name($this->table)->where($where)->data($data)->update();
        } catch (\Exception $e) {
            $result = false;
        }
        
        return $result;
    }
}
